package com.ym.excel.domain;

import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 读取Excel
 */
public class ExcelReader {
    private ExcelReader() {}

    /**
     * 读取Excel内容
     * @param path
     * @param sheetName
     * @param hasTitle
     * @return
     * @throws Exception
     */
    public static List<List<Object>> readExcel (String path, String sheetName, boolean hasTitle) throws Exception{
        Sheet sheet = getSheet(path, sheetName);
        List<List<Object>> resultList = new ArrayList<List<Object>>();
        int totalRow = sheet.getLastRowNum();
        int startRow = 0;
        if (totalRow <= 0) {
            return null;
        }
        if (hasTitle) {
            resultList.add(rowInfoRead(sheet, 0));
            startRow += 1;
        }
        List<Object> dataList = new ArrayList<Object>();
        for(; startRow <= totalRow; startRow++) {
            dataList.add(rowInfoRead(sheet, startRow));
        }
        resultList.add(dataList);

        return resultList;
    }

    /**
     * 读取Excel内容，若title 为空则将Excel 第一行作为相应的title使用
     * @param path
     * @param title
     * @return
     * @throws Exception
     */
    public static List<Map<String, String>> readExcel(String path, String sheetName, List<String> title) throws Exception{
        Sheet sheet = getSheet(path, sheetName);
        List<Map<String, String>> resultList = new ArrayList<Map<String, String>>();
        int totalRow = sheet.getLastRowNum();
        int startRow = 0;
        if (totalRow <= 0) {
            return null;
        }
        if (title == null) {
            title = rowInfoRead(sheet,0);
            startRow += 1;
        }
        for (; startRow <= totalRow; startRow++) {
            Map<String, String> rowMap = rowInfoRead(sheet, startRow, title);
            resultList.add(rowMap);
        }

        return resultList;
    }

    /**
     * 获取 sheet 对象
     * @param path
     * @param sheetName
     * @return
     * @throws Exception
     */
    private static Sheet getSheet(String path, String sheetName) throws Exception{
        File file = new File(path);
        if (file == null || !file.exists()) {
            throw new Exception("文件不存在");
        }
        Workbook workbook = WorkbookFactory.create(file);
        Sheet sheet;
        if (sheetName != null && !"".equals(sheetName.trim())) {
            return workbook.getSheet(sheetName);
        }
        sheet = workbook.getSheetAt(0);
        return sheet;
    }

    /**
     * 读取行数据
     * @param sheet
     * @param startIndex
     * @param title
     * @return
     * @throws Exception
     */
    private static Map<String, String> rowInfoRead(Sheet sheet, int startIndex, List<String> title) throws Exception {
        Map<String, String> resultMap = new HashMap<String, String>();
        Row row = sheet.getRow(startIndex);
        short totalCell = row.getLastCellNum();
        for (int i = 0; i < totalCell; i++) {
            Cell cell = row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
            String content = cell.getStringCellValue();
            resultMap.put(title.get(i), content);
        }
        return resultMap;
    }

    /**
     * 读取行数据
     * @param sheet
     * @param rowIndex
     * @return
     * @throws Exception
     */
    private static List rowInfoRead(Sheet sheet, int rowIndex) throws Exception {
        Row row = sheet.getRow(rowIndex);
        short maxColIx = row.getLastCellNum(); // 最后一列
        List list = new ArrayList();
        for(short colIx = 0; colIx < maxColIx; colIx++) {
            try {
                Cell cell = row.getCell(colIx, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                String content = cell.getStringCellValue();
                list.add(content);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }

}
